//
//  WBMianViewController.swift
//  徐工微博
//
//  Created by ahfu on 2017/4/8.
//  Copyright © 2017年 ahfu. All rights reserved.
//

import UIKit

class WBMianViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupChildControllers()
        setupComposeButton()
    }
    /**
     portrait : 竖屏 肖像
     landspace : 横屏 风景画
     使用代码控制设备的方向，好处，可以在需要竖屏的时候，单独处理
     设置支持的方向后，当前的控制器及子控制器都会遵守这个方向！
     如果播放视屏，通常是通过modal 展现的
     */
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
        return .portrait
    }
    override func viewWillAppear(_ animated: Bool) {
        
    }
    
    @objc fileprivate func composeStatus() {
        print("发表微博")
        //测试方向旋转
        let vc = UIViewController()
        vc.view.backgroundColor = UIColor.radomColor()
        let nav = UINavigationController(rootViewController: vc)
        present(nav, animated: true,completion: nil)
    }
    
    //MARK: -
    /**
     /Users/ahfu/Documents/weibo/徐工微博/徐工微博/Classes/View/Main/WBMianViewController.swift:15:9: 'setupChildControllers' is inaccessible due to 'private' protection level
     */
    fileprivate lazy var composeButton :UIButton = UIButton()
        //.setImage(UIImage(named: "tabbar_compose_icon_add"), for: .normal)
    
        //composeButton.image = UIImage(named: "tabbar_compose_icon_add")
}
extension WBMianViewController {
    
    fileprivate func setupComposeButton(){
        composeButton.setImage(UIImage(named: "tabbar_compose_icon_add"), for: .normal)
        composeButton.setBackgroundImage(UIImage(named: "tabbar_compose_button"), for: .normal)
        composeButton.sizeToFit()
        //计算按钮的宽度
        //let count = childViewControllers.count
        let w = tabBar.bounds.width / 5 - 1
        //设置按钮的位置
        composeButton.frame = tabBar.bounds.insetBy(dx: 2 * w, dy: 0)

        print(w)
        tabBar.addSubview(composeButton)
        print("######")
        
        composeButton.addTarget(self , action: #selector(composeStatus), for: .touchUpInside)
    }
    fileprivate func setupChildControllers(){
        let array = [
            ["clsName": "WBHomeViewController","title": "首页", "imageName": "home"],
            ["clsName": "WBMessageViewController","title": "消息", "imageName": "message_center"],
            ["clsName": "UIViewController"],
            ["clsName": "WBDiscoverViewController","title": "发现", "imageName": "discover"],
            ["clsName": "WBProfileViewController","title": "我", "imageName": "profile"],
            ]
        
        var arrayM = [UIViewController]()
        for dict in array {
            arrayM.append(controller(dict: dict))
        }
        
        viewControllers = arrayM
    }
    
    fileprivate func controller(dict: [String: String]) -> UIViewController{
        guard let clsName = dict["clsName"],let title = dict["title"],let imageName = dict["imageName"],let cls = NSClassFromString(Bundle.main.namespace + "." + clsName) as? UIViewController.Type else {
            return UIViewController()
        }
        //2. 创建视图控制器
        let vc = cls.init()
        vc.title = title
        
        //3. 设置图像
        vc.tabBarItem.image = UIImage(named: "tabbar_" + imageName)
        vc.tabBarItem.selectedImage = UIImage(named: "tabbar_" + imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
        
        //4. 设置字体
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: .highlighted)
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 12)], for: .normal)
        
        
        
        let nav = WBNavigationController(rootViewController: vc)
        return nav
        
    }
}
